package com.share.two.Pub_Sub;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.jms.Topic;

import org.apache.activemq.ActiveMQConnectionFactory;

public class Subscribe3 {
	
	public static void main(String[] args) throws Exception{
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
				"sunxj", 
				"123123", 
				"tcp://localhost:61616");
		Connection connection = connectionFactory.createConnection();
		connection.setClientID("bbb");
		connection.start();
		
		Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		Topic destination = session.createTopic("topic1");
		//MessageConsumer messageConsumer = session.createConsumer(destination);

		MessageConsumer messageConsumer = session.createDurableSubscriber(destination, "bbb");
		messageConsumer.setMessageListener(new MessageListener() {
			
			@Override
			public void onMessage(Message message) {
				try {
					//System.out.println("订阅者2： 异步接收");
					TextMessage textMessage = (TextMessage)message;
					System.out.println("订阅者2： " + textMessage.getText());
				} catch (Exception e) {
					System.out.println(e);
				}
				
			}
		});

		//System.out.println("主线程");
	}

}
